'''
@author: haiwen
@date: 2020/9/25
@file: pulgins.py
'''
import datetime
from functools import wraps

import allure
import yaml
#处理配置文件
class Config():
    @staticmethod
    def read_yml(path):
        with open(path,encoding='utf8') as f:
            content=yaml.safe_load(f.read())
        return content

    @staticmethod
    def write_yml(path,data):
        with open(path,'w',encoding='utf8') as f:
            yaml.safe_dump(data,f)

class ConvertData():
    @staticmethod
    def current_time(time_formate='%Y-%m-%dT%H:%M:%S.%f'):
        # 获取当前时间
        dt = datetime.datetime.now()
        # 格式化时间
        cur_time = dt.strftime(time_formate)
        return cur_time[:-3] + 'Z'

    @staticmethod
    def get_params():
        content = Config.read_yml('case_data/TestContracts.yml')
        print(content['add'])
        data = [content['add'][key] for key in content['add']]
        print(data)
        # 转化成[('收款合同',1900).....]
        res = []
        for i in range(len(data[0])):
            # 收集元素
            ele = []
            for k in range(len(data)):
                # 拼装字元素
                ele.append(data[k][i])
            res.append(ele)
        return res

def dynamic_report(target):
    def decorate(func):
        @wraps(func)  #目的是保留原函数的名称
        def warpper(*args,**kw):
            func(*args,**kw)
            print('执行测试报告定制化工作')
            allure.dynamic.title(kw[target])
        return warpper
    return decorate


if __name__ == '__main__':
    test_data = {"testapi": {"url": "/abc/def", "payload": {"name": "tom", "password": "123@qwe"}}}
    test_file = 'demo.yml'

    test_data['testapi']['url'] = '/user/login'
    Config.write_yml(test_file, test_data)
    res = Config.read_yml(test_file)
    print(res)